var loanId = $("#loan").data("id"),
    loanStatus = $("#loan").data("status"),
    loanTime = $("#loan").data("loantime");

$(function () {

    var loadRepays = function () {
        var lang = chinese;
        chinese.sInfo = "共 _TOTAL_ 人投标";
        chinese.sInfoEmpty = "";
        // 初始化列表
        var table = $('#repayTable').DataTable({
            //"processing": true,
            //"serverSide": true,
            "ajax": "repayment/getLoanRepays/" + loanId,
            "aoColumnDefs": [
                {
                    "targets": 0, // 期数
                    "className": 'details-control',
                    "mRender": function (data, type, row) {
                        return '<span class="glyphicons glyphicons-circle_plus text-system cursor-pointer fs14"></span>';
                    }
                }, {
                    "targets": 1, // 期数
                    "mRender": function (data, type, row) {
                        return row.period;
                    }
                }, {
                    "targets": 2, // 应还总额
                    "mRender": function (data, type, row) {
                        return row.repayment.total;
                    }
                }, {
                    "targets": 3, // 应还本金
                    "mRender": function (data, type, row) {
                        return $.number(row.repayment.principal, 2);
                    }
                }, {
                    "targets": 4, // 应还利息
                    "mRender": function (data, type, row) {
                        return $.number(row.repayment.interest, 2);
                    }
                }, {
                    "targets": 5, // 剩余本金
                    "mRender": function (data, type, row) {
                        return $.number(row.repayment.outstanding, 2);
                    }
                }, {
                    "targets": 6, // 截止日期
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        return row.dueDate;
                    }
                }, {
                    "targets": 7, // 实际还款金额
                    "mRender": function (data, type, row) {
                        return $.number(row.repayAmount, 2);
                    }
                }, {
                    "targets": 8, // 实际还款日期
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        if (row.repayTime) {
                            var date = new Date(row.repayTime);
                            return $.format.date(date, "yyyy-MM-dd HH:mm");
                        } else {
                            return '-';
                        }
                    }
                }, {
                    "targets": 9, // 状态
                    "searchable": false,
                    "mRender": function (data, type, row) {
                        return sysRepayStatus[row.status];
                    }
                }],
            "order": [1, 'asc'],
            "oLanguage": chinese,
            "language": {
                "decimal": ",",
                "thousands": ",",
            },
            "iDisplayLength": 1000,
            "aLengthMenu": [
                [5, 10, 25, 50, 100, -1],
                [5, 10, 25, 50, 100, "全部"]
            ],
            "sDom": '<"dt-panelmenu clearfix"<"repay-table-title pull-left mt5 fw600"><"pull-right">r>t',
            "oTableTools": {
                "aButtons": [{
                        "sExtends": "xls",
                        "sButtonText": "导出为Excel"
                    }
                ],
                "sSwfPath": root_path + "/js/plugins/datatables/extensions/TableTools/swf/copy_csv_xls_pdf.swf"
            },
            "initComplete": function (settings, json) {
                $(".repay-table-title").html('<i class="fa fa-table mr5"></i><span class="fs14">还款计划详情</span><code class="investCount">共' + json.recordsTotal + '期</code>');
            }
        });
        /**
         * 详情展开/关闭
         */
        $('#repayTable tbody').on('click', 'td.details-control', function () {
            var tr = $(this).closest('tr');
            var row = table.row(tr);

            if (row.child.isShown()) {
                // 关闭详情
                $(this).html('<span class="glyphicons glyphicons-circle_plus text-system cursor-pointer fs14"></span>');
                row.child.hide();
                tr.removeClass('shown');
            }
            else {
                // 展开详情
                $(this).html('<span class="glyphicons glyphicons-circle_minus text-info cursor-pointer fs14"></span>');
                $.get("repayment/listInvestRepays/" + row.data().id, function (res) {
                    var detail = sys.render("#investRepayTemplate", {result: res});
                    row.child(detail).show();
                });
                tr.addClass('shown');
            }
        });
    };

    var loadRepayPlan = function () {
        var index = 1;
        $.get("repayment/getRepayPlan/" + loanId, function (res) {
            if (res) {
                res.getDueDate = function () {
                    var dueDate = this.dueDate;
                    return $.format.date(new Date(dueDate.year, dueDate.monthValue - 1, dueDate.dayOfMonth), 'yyyy-MM-dd');
                };
                res.index = function () {
                    return index++;
                }
                var plan = sys.render("#investRepayPlanTemplate", res);
                $("#repayContainer").html(plan);
            }
        });
    };
    var initPage = function () {
        // 更新tab状态
        $("#tab-repays").removeClass("btn-default").addClass("btn-primary");
        // 更新标的列表链接
        $(".returnLoanListLink").attr("href", "bill/list/" + loanStatus.toLowerCase());
        // 加载还款计划
        if (loanTime !== '') {
            loadRepays();
        } else {
            loadRepayPlan();
        }
    };

    // 初始化页面
    initPage();
});
